#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

void handler(int sig) {
  if (sig == SIGHUP) {
    FILE* fp = fopen("tmp", "a");
    fprintf(fp, "[%d]: Hello SIGHUP, my father is [%d]\n", getpid(), getppid());
    fclose(fp);
    exit(0);
  }
}

int main() {
  signal(SIGHUP, handler);
  pid_t pid = fork();
  if (pid == 0) {
    // setpgid(getpid(), getpid());
    pid = fork();
    if (pid == 0) { 
      printf("I'm %d, I'm stopped", getpid());
      //kill(getpid(), SIGSTOP); // 子进程暂停
    }
    while(1) {
      printf("I'm %d, father %d\n", getpid(), getppid());
      sleep(1);
    }
  }
  printf("main: I'm %d, father %d\n", getpid(), getppid());

  while(1) sleep(5);
  return 0;
}
